package org.ant.gui.bar;

import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JToolBar;

import org.ant.gui.actionListeners.NewActionListener;
import org.ant.gui.actionListeners.OpenActionListener;
import org.ant.gui.actionListeners.RedoActionListener;
import org.ant.gui.actionListeners.RunActionListener;
import org.ant.gui.actionListeners.SaveActionListener;
import org.ant.gui.actionListeners.SaveAsActionListener;
import org.ant.gui.actionListeners.ThreeAddressActionListener;
import org.ant.gui.actionListeners.UndoActionListener;
import org.ant.gui.layout.GBC;

public class AntToolBar extends JToolBar {

	private static final int INSET = 2;

	private JButton newButton;
	private JButton openButton;
	private JButton saveButton;
	private JButton saveAsButton;
	private JButton undoButton;
	private JButton redoButton;
	private JButton runButton;
	private JButton tacButton;

	public AntToolBar() {
		super();
		init();
		loadComponents();
	}

	protected void init() {
		setBorderPainted(false);
		setFloatable(false);
		setLayout(new GridBagLayout());
	}

	protected void loadComponents() {
		add(getNewButton(), new GBC(0, 0, 1, 1).setInset(INSET));
		add(getOpenButton(), new GBC(1, 0, 1, 1).setInset(INSET));
		add(getSaveButton(), new GBC(2, 0, 1, 1).setInset(INSET));
		add(getSaveAsButton(), new GBC(3, 0, 1, 1).setInset(INSET));
		add(getUndoButton(), new GBC(4, 0, 1, 1).setInset(INSET));
		add(getRedoButton(), new GBC(5, 0, 1, 1).setInset(INSET));
		add(getRunButton(), new GBC(6, 0, 1, 1).setInset(INSET));
		add(getTACButton(), new GBC(7, 0, 1, 1).setInset(INSET));
		add(new JPanel(), new GBC(7, 0, 1, 1).setWeight(1, 0));
	}

	public JButton getNewButton() {
		if (newButton == null) {
			newButton = new JButton();
			// newButton.setBorderPainted(false);
			Icon icon = new ImageIcon("icon/new.gif");
			newButton.setIcon(icon);
			newButton.addActionListener(new NewActionListener());
		}
		return newButton;
	}

	public JButton getOpenButton() {
		if (openButton == null) {
			openButton = new JButton();
			// openButton.setBorderPainted(false);
			Icon icon = new ImageIcon("icon/open.gif");
			openButton.setIcon(icon);
			openButton.addActionListener(new OpenActionListener());
		}
		return openButton;
	}

	public JButton getSaveButton() {
		if (saveButton == null) {
			saveButton = new JButton();
			// saveButton.setBorderPainted(false);
			Icon icon = new ImageIcon("icon/save.gif");
			saveButton.setIcon(icon);
			saveButton.addActionListener(new SaveActionListener());
		}
		return saveButton;
	}

	public JButton getSaveAsButton() {
		if (saveAsButton == null) {
			saveAsButton = new JButton();
			// saveAsButton.setBorderPainted(false);
			Icon icon = new ImageIcon("icon/saveas.gif");
			saveAsButton.setIcon(icon);
			saveAsButton.addActionListener(new SaveAsActionListener());
		}
		return saveButton;
	}

	public JButton getUndoButton() {
		if (undoButton == null) {
			undoButton = new JButton();
			// undoButton.setBorderPainted(false);
			Icon icon = new ImageIcon("icon/undo.gif");
			undoButton.setIcon(icon);
			undoButton.addActionListener(new UndoActionListener());
		}
		return undoButton;
	}

	public JButton getRedoButton() {
		if (redoButton == null) {
			redoButton = new JButton();
			// redoButton.setBorderPainted(false);
			Icon icon = new ImageIcon("icon/redo.gif");
			redoButton.setIcon(icon);
			redoButton.addActionListener(new RedoActionListener());
		}
		return redoButton;
	}

	public JButton getRunButton() {
		if (runButton == null) {
			runButton = new JButton();
			// runButton.setBorderPainted(false);
			Icon icon = new ImageIcon("icon/run.gif");
			runButton.setIcon(icon);
			runButton.addActionListener(new RunActionListener());
		}
		return runButton;
	}

	public JButton getTACButton() {
		if (tacButton == null) {
			tacButton = new JButton();
			// runButton.setBorderPainted(false);
			Icon icon = new ImageIcon("icon/tac.gif");
			tacButton.setIcon(icon);
			tacButton.addActionListener(new ThreeAddressActionListener());
		}
		return tacButton;
	}
}
